<template>
  <BasicModal v-bind="$attrs" destroyOnClose title="会员卡升级" :width="modalWidth" :okButtonProps="{ disabled: false }" @register="registerModal" :destroyOnClose="true" @ok="handleSubmit">
    <BasicForm @register="registerForm"></BasicForm>
    <Description size="middle" class="mt-4" title="会员卡信息" :column="{ xxl: 4, xl: 3, lg: 3, md: 3, sm: 2, xs: 1 }" :data="userGoodsInfo.data" :schema="userGoodsInfoSchema" />
  </BasicModal>
</template>
<script lang="ts" setup>
import { BasicModal, useModalInner } from '@apps/antd-admin/src/components/modal';
import { BasicForm, useForm } from '@apps/antd-admin/src/components/form';
import { DescItem, Description } from '@apps/antd-admin/src/components/description';
import { upgradeFormSchema } from '../data/data';
import { reactive, ref, unref } from 'vue';
import { getUserGoodsDetail, userGoodsUpgradeSub } from '../data/api';
import { useAppInject } from '@apps/antd-admin/src/hooks/web/use-app-inject';
const { getIsMobile } = useAppInject();
const modalWidth = ref('750px');
if (unref(getIsMobile)) {
  modalWidth.value = '80%';
}
const emit = defineEmits(['success', 'register']);

const userGoodsInfoSchema: DescItem[] = [
  {
    field: 'vipCardState',
    label: '会员卡状态',
    span: 3,
    render: (curVal, data) => {
      let vipCardStateArray = [
        { label: '待激活', value: 0 },
        { label: '正常', value: 1 },
        { label: '已过期', value: 2 },
        { label: '未开通', value: 3 },
        { label: '未通过', value: 4 },
        { label: '冻结', value: 5 },
        { label: '退卡中', value: 6 },
        { label: '已退卡', value: 7 },
        { label: '已转卡', value: 8 },
        { label: '已转赠', value: 9 },
      ];
      const vipCardState: any = vipCardStateArray.find((item) => item.value == curVal);
      if (vipCardState) {
        return vipCardState.label;
      }
      return '';
    },
  },
  {
    field: 'vipCardNo',
    label: '会员卡号',
    span: 3,
  },
  {
    field: 'goodsName',
    label: '卡种名称',
    span: 3,
  },
  {
    field: 'vipCardNumber',
    label: '卡种编号',
    span: 3,
  },
  {
    field: 'buyDate',
    label: '开卡日期',
    span: 3,
  },
  {
    field: 'payAmount',
    label: '支付金额',
    span: 3,
  },
  {
    field: 'paywayName',
    label: '支付方式',
    span: 3,
  },
];
const [registerForm, { resetFields, setFieldsValue, validate }] = useForm({
  labelWidth: 90,
  schemas: upgradeFormSchema,
  showActionButtonGroup: false,
});
const isUpdate = ref(true);
let userGoodsInfo: Recordable = reactive({ data: {} });
const [registerModal, { setModalProps, closeModal }] = useModalInner(async (data) => {
  resetFields();
  data.value = !!data?.isUpdate;
  let record = data.record;
  let queryData = await getUserGoodsDetail({
    userGoodsId: record.userGoodsId,
    handlerName: 'appUserGoodsGetHandler',
  });
  userGoodsInfo.data = queryData;
  setFieldsValue({ userGoodsId: queryData.userGoodsId, oldGoodsId: queryData.goodsId });
});

async function handleSubmit() {
  try {
    const values = await validate();
    console.info(values);
    setModalProps({ confirmLoading: true });
    if (unref(isUpdate)) {
      await userGoodsUpgradeSub(values);
    }
    closeModal();
    emit('success', false);
  } finally {
    setModalProps({ confirmLoading: false });
  }
}
</script>
